import Clipboard from 'clipboard'
import { showToast } from 'vant';
export const useUtil = () => {
    let clipboard: any = null
    const clipboardInit = (content: any, clickEvent: any) => {
        clipboard = new Clipboard(clickEvent.target, {
            text: () => content
        })
        clipboard.on('success', () => {
            showToast({
                message: '复制成功',
                position: 'bottom',
            });
            clipboardReset()
        })
        clipboard.on("error", () => {
            showToast({
                message: '复制失败',
                position: 'bottom',
            });
            clipboardReset()
        })
    }
    const clipboardReset = () => {
        clipboard.destroy()
        clipboard = null
    }
    const copyToClipboard = (content: any, clickEvent: any) => {
        clipboardInit(content, clickEvent)
        clipboard.onClick(clickEvent)
    }
    return {
        copyToClipboard
    }

} 
